package com.zebralabs.demo;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

import javax.servlet.ServletException;
import java.io.File;
import java.net.MalformedURLException;

public class EmbeddedTomcat8 {
    public static void main(String[] args) throws LifecycleException, ServletException, MalformedURLException {
        // 设置端口号，可按需修改
        int port = 8080;
        String docBase = "C:\\Users\\ASUS\\Desktop\\gitee\\tomcat-embed-sample\\src\\main\\webapp";

        for (String arg : args) {
            if (arg.startsWith("--port=")) {
                port = Integer.parseInt(arg.substring(7));
            }

            if (arg.startsWith("--base=")) {
                docBase = arg.substring(7);
            }
        }

        System.out.println("Starting Tomcat on port " + port);
        System.out.println("base: " + docBase);

        // 创建Tomcat实例
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(port);

        // 创建一个Web应用上下文
        Context context = tomcat.addWebapp("/", docBase);

        // 启动Tomcat
        tomcat.start();

        // 让Tomcat一直运行，等待请求处理，这里简单阻塞主线程，实际可能需要更完善的机制
        tomcat.getServer().await();
    }
}